import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisDemo {
    public static void test1(Jedis jedis) {
        System.out.println("get 和 set 的使用");
        //先清空数据库,要避免上一组测试的数据影响到下一组测试的结果
        jedis.flushAll();

        jedis.set("key","111");
        jedis.set("key2","222");

        SetParams params = new SetParams();
        params.ex(10);
        params.nx();
        jedis.set("key3","333", params);

        String value = jedis.get("key");
        System.out.println(value);
    }

    public static void test2(Jedis jedis) {
        System.out.println("exists 和 del");
        jedis.flushAll();

        jedis.set("key", "111");
        jedis.set("key2","222");
        System.out.println(jedis.exists("key"));
        System.out.println(jedis.del("key3"));
        boolean key = jedis.exists("key");
        System.out.println(key);

        long result = jedis.del("key", "key2", "key3", "key4");
        System.out.println(result);
    }

    public static void test3(Jedis jedis) {
        System.out.println("keys 的使用");
        jedis.flushAll();

        jedis.set("key", "111");
        jedis.set("key2","222");
        jedis.set("key3","333");
        jedis.set("key4","333");
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
    }

    public static void test4(Jedis jedis) throws InterruptedException {
        System.out.println("expire 和 ttl 的使用");
        jedis.flushAll();

        jedis.set("key", "111");
        jedis.set("key2","222");
        jedis.set("key3","333");
        jedis.set("key4","333");

        jedis.expire("key", 10);
        Thread.sleep(3000);
        long time = jedis.ttl("key");
        System.out.println(time);
    }

    public static void test5(Jedis jedis) throws InterruptedException {
        System.out.println("type 的使用");
        jedis.flushAll();

        jedis.set("key", "111");

        String type = jedis.type("key");
        System.out.println("type: " + type);

        jedis.lpush("key2","111","222","333");
        type = jedis.type("key2");
        System.out.println("type: " + type);

        jedis.sadd("key3","1","2","3");
        type = jedis.type("key3");
        System.out.println("type: " + type);

        jedis.hset("key4","f1","111");
        type = jedis.type("key4");
        System.out.println("type: " + type);

        jedis.zadd("key5", 10, "zhangsan");
        type = jedis.type("key4");
        System.out.println("type: " + type);
    }

    public static void main(String[] args) {
        //连接到 Redis 服务器上
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        //从 redis 连接池中取一个连接出来, 连接用完之后要记得释放(close), 使用try语句会自动释放
        try(Jedis jedis = jedisPool.getResource()) {
            // redis 的各自命令, 就对应到 jejis 对象的各种方法
//            String pong = jedis.ping();
//            System.out.println(pong);
            test5(jedis);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
